﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

using Microsoft.VisualBasic.Logging;


namespace EasySL.Server
{
    public class Logger
    {
        public static void Init(bool logToConsole, string logFolder)
        {
            _LogToConsole = logToConsole;

            if (_FileLogger == null && !string.IsNullOrEmpty(logFolder))
            {
                _FileLogger = new FileLogTraceListener();
                _FileLogger.BaseFileName = "Journal";
                _FileLogger.Append = true;
                _FileLogger.IncludeHostName = false;
                _FileLogger.Delimiter = ", ";
                _FileLogger.AutoFlush = true;
                _FileLogger.TraceOutputOptions |= TraceOptions.DateTime | TraceOptions.Timestamp;

                _FileLogger.Location = LogFileLocation.Custom;
                _FileLogger.CustomLocation = logFolder;

                _FileLogger.LogFileCreationSchedule = LogFileCreationScheduleOption.Daily;
                _FileLogger.MaxFileSize = 10 * 1024 * 1024;// 10M
                _FileLogger.DiskSpaceExhaustedBehavior = DiskSpaceExhaustedOption.ThrowException;
            }
        }

        public static void UnInit()
        {
            if (_FileLogger != null)
            {
                _FileLogger.Close();
            }
        }


        public static void LogTextLine(string text)
        {
            if (_FileLogger != null)
            {
                _FileLogger.WriteLine(text);
            }

            if (_LogToConsole)
            {
                Console.WriteLine(text);
            }
        }

        public static void LogBlankLine()
        {
            LogTextLine(string.Empty);
        }

        public static void LogException(Exception ex)
        {
            LogError(string.Format("Exception: {0}", ex.Message));
            LogError(string.Format("Exception Source: {0}", ex.Source));
            LogError(string.Format("Exception Callstack: {0}", ex.StackTrace));
        }

        public static void LogInformation(string msg)
        {
            LogToFile(Logger._InfoTypeString, msg);
            LogToConsole(Logger._InfoTypeString, msg);

        }

        public static void LogError(string msg)
        {
            LogToFile(Logger._ErrorTypeString, msg);
            LogToConsole(Logger._ErrorTypeString, msg);
        }

        public static void LogWarning(string msg)
        {
            LogToFile(Logger._WarningTypeString, msg);
            LogToConsole(Logger._WarningTypeString, msg);
        }

        private static void LogToFile(string logType, string msg)
        {
            if (_FileLogger != null)
            {
                _FileLogger.WriteLine(string.Format("[{0}] {1} - {2}", DateTime.Now.ToString(), logType, msg));
            }
        }

        private static void LogToConsole(string logType, string msg)
        {
            if (_LogToConsole)
            {
                string typeStr = _InfoTypeString;
                ConsoleColor original = Console.ForegroundColor;
                ConsoleColor typeColor = ConsoleColor.Cyan;

                if (logType == _ErrorTypeString)
                {
                    typeStr = _ErrorTypeString;
                    typeColor = ConsoleColor.Red;
                }
                else if (logType == _WarningTypeString)
                {
                    typeStr = _WarningTypeString;
                    typeColor = ConsoleColor.Green;
                }

                try
                {
                    Console.ForegroundColor = typeColor;
                }
                catch
                {
                }

                Console.WriteLine(string.Format("[{0}] {1} - {2}", DateTime.Now.ToString(), typeStr, msg));

                try
                {
                    Console.ForegroundColor = original;
                }
                catch
                {
                }
            }
        }

        private static string _ErrorTypeString = "ERROR";
        private static string _WarningTypeString = "WARNING";
        private static string _InfoTypeString = "INFO";

        private static FileLogTraceListener _FileLogger = null;
        private static bool _LogToConsole = true;

    }
}
